﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Telerik.Web.UI;
using System.Collections.Specialized;
using System.Data;
using SalePartial.Utility;
using XunPan.Web.Globals;
using System.Text.RegularExpressions;
using SalePartial.BLL;
using SalePartial.BLL.SAServiceRef;
using SalePartial.Model;
using System.Data.SqlClient;
public partial class SAClientWeb_MainFrm : System.Web.UI.Page
{
    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            DoRefreshCategoryView();
            DoRefreshVendorView();
        }
    }

    private void DoRefreshCategoryView()
    {
        try
        {
            //update categories.
            if (treeView_Category == null)
            {
                return;
            }
            //this.treeView_category.AllowDrop = true;
            this.treeView_Category.Nodes.Clear();


            List<CategoryInfo> cateInfos = SABLL.GetRootCategories();

            if (cateInfos == null)
            {
                return;
            }

            for (int i = 0; i < cateInfos.Count; i++)
            {
                CategoryInfo curCateInfo = cateInfos[i];

                AddNodeByCategoryInfo(this.treeView_Category.Nodes, curCateInfo);
            }

            this.treeView_Category.ExpandAllNodes();

            DoRefreshFamilyListView();
           
        }
        catch (System.Exception ex)
        {
            SABLL.ShowErrMsg(this, ex.Message);
        }
    }

    private  void AddNodeByCategoryInfo(RadTreeNodeCollection nodes, CategoryInfo cateInfo)
    {
        //add this node.
        RadTreeNode newNode = new RadTreeNode();
        newNode.Text = cateInfo.Name;
        newNode.Value = cateInfo.Id.ToString();
        nodes.Add(newNode);

        //add sub node.
        //get sub nodes.
        List<CategoryInfo> cateInfos = SABLL.GetSubCategories(cateInfo);

        if (cateInfos == null)
        {
            return;
        }

        foreach (CategoryInfo tempCateInfo in cateInfos)
        {
            AddNodeByCategoryInfo(newNode.Nodes, tempCateInfo);
        }
    }



    private void DoRefreshVendorView()
    {
        try
        {
            //update categories.
           
            if (treeView_Vendor == null)
            {
                return;
            }
            //this.treeView_category.AllowDrop = true;
            this.treeView_Vendor.Nodes.Clear();


            List<VendorInfo> venderInfos = SABLL.GetAllVendors();

            if (venderInfos == null)
            {
                return;
            }

            List<CategoryInfo> cateInfos = SABLL.GetRootCategories();

            for (int i = 0; i < venderInfos.Count; i++)
            {
                VendorInfo vendorInfo = venderInfos[i];

                //now add categories.
                
                for (int j = 0; j < cateInfos.Count; j++)
                {
                    CategoryInfo curCateInfo = cateInfos[j];

                    RadTreeNode newNode = new RadTreeNode();
                    newNode.Text = vendorInfo.Name;
                    newNode.Value = vendorInfo.VendorId.ToString();
                    treeView_Vendor.Nodes.Add(newNode);

                    bool bHasFamily = SABLL.IsCategoryContainsFamilyFromVendor(curCateInfo, vendorInfo.VendorId, true);

                    if (bHasFamily)
                    {
                        AddVendorCategoryNode(newNode, vendorInfo.VendorId, curCateInfo);
                    }
                }
            }

            this.treeView_Vendor.ExpandAllNodes();

            DoRefreshFamilyListView();
        }
        catch (System.Exception ex)
        {
            SABLL.ShowErrMsg(this, ex.Message);
        }
    }


    private  void AddVendorCategoryNode(RadTreeNode parentNode, int vendorId, CategoryInfo categoryInfo)
    {
        if (parentNode == null)
        {
            return;
        }

        //add this node.
        RadTreeNode newNode = new RadTreeNode();
        newNode.Text = categoryInfo.Name;
        //CategoryVendorHelpInfo cvInfo = new CategoryVendorHelpInfo();
        //cvInfo.cateID = categoryInfo.Id.ToString();
        //cvInfo.vendorID = vendorId;
        //newNode.Tag = cvInfo;
        newNode.Attributes.Add("cateID", categoryInfo.Id.ToString());
        newNode.Attributes.Add("vendorID", vendorId.ToString());

        parentNode.Nodes.Add(newNode);

        List<CategoryInfo> cateInfos = SABLL.GetSubCategories(categoryInfo);

        if (cateInfos == null)
        {
            return;
        }

        for (int i = 0; i < cateInfos.Count; i++)
        {
            CategoryInfo curCateInfo = cateInfos[i];

            bool bHasFamily = SABLL.IsCategoryContainsFamilyFromVendor(curCateInfo, vendorId, true);

            if (bHasFamily)
            {
                AddVendorCategoryNode(newNode, vendorId, curCateInfo);
            }
        }
    }

    private void DoRefreshFamilyListView()
    {
        RadTreeNode node = this.treeView_Category.SelectedNode;

        if (node == null) return;

        //CategoryInfo curCateInfo = node.Tag as CategoryInfo;
        List<FamilyInfo> families = SABLL.GetFamiliesByCategoryId(new Guid(node.Value));
        List<SAImageInfo> familyImages = SABLL.GetFamilyImageByFamilies(families);
        ViewState["data"] = familyImages;
        bindGridData(true); 
    }


  protected void treeView_Category_AfterSelect(object sender, RadTreeNodeEventArgs e)
  {
      //DoRefreshFamilyListView();
      string Categoryid= "";
      RadTreeNode node = this.treeView_Category.SelectedNode;
      IList<RadTreeNode> childNodes = node.GetAllNodes();
      childNodes.Add(node);
      foreach (RadTreeNode item in childNodes)
      {
          if (item != null)
          {
              Categoryid = Categoryid + "CategoryID ='" + item.Value + "'  or   ";

          }
          List<FamilyInfo> families = SABLL.GetFamiliesByCategoryIdnew(new Guid(item.Value), Categoryid);
          List<SAImageInfo> familyImages = SABLL.GetFamilyImageByFamilies(families);
          ViewState["data"] = familyImages;
          bindGridData(true);
      }
  }

  protected void treeView_Vendor_AfterSelect(object sender, RadTreeNodeEventArgs e)
  {
      
      RadTreeNode node = this.treeView_Vendor.SelectedNode;
      string searchString = "";
      if (node != null)
      {
          List<FamilyInfo> families = new List<FamilyInfo>();
      int vendorId = 0;
          if (node.Value != "")
          {
              
              int.TryParse(node.Value, out vendorId);

              families = SABLL.GetFamiliesByVendorId(vendorId,searchString);
          }
          else
          {
              string cateID = node.Attributes["cateID"];
              int.TryParse(node.Attributes["vendorID"], out vendorId);
              if (cateID != null && vendorId!=0)
              {
                  families = SABLL.GetFamiliesByCategoryVendorId(new Guid(cateID), vendorId,searchString);
              }
          }

          ViewState["data"] = SABLL.GetFamilyImageByFamilies(families);
          bindGridData(true);
      }
  }

  protected void listView_Families_NeedDataSource(object source, RadListViewNeedDataSourceEventArgs e)
  {
      bindGridData(false);
  }

  private void bindGridData(bool rebind)
  {
      if (ViewState["data"] != null)
      {
          this.listView_Families.DataSource = (List<SAImageInfo>)ViewState["data"];
          if (rebind)
          {
              this.listView_Families.DataBind();
          }
      }
  }

  protected void button_Search_Click(object sender, EventArgs e)
  {
      try
      {

          string searchString = this.textBox_Search.Text.ToString().ToUpper();
          if (String.IsNullOrEmpty(searchString))
          {
              List<FamilyInfo> families = SABLL.SearchFamilies(searchString);
              ViewState["data"] = SABLL.GetFamilyImageByFamilies(families);
              bindGridData(true);
          }


          //ning_start
          RadTreeNode node = this.treeView_Vendor.SelectedNode;

          if (node != null)
          {
              List<FamilyInfo> families = new List<FamilyInfo>();
              int vendorId = 0;


              if (node.Value != "")
              {

                  int.TryParse(node.Value, out vendorId);

                  families = SABLL.GetFamiliesByVendorId(vendorId, searchString);
              }
              else
              {
                  string cateID = node.Attributes["cateID"];
                  int.TryParse(node.Attributes["vendorID"], out vendorId);
                  if (cateID != null && vendorId != 0)
                  {
                      families = SABLL.GetFamiliesByCategoryVendorId(new Guid(cateID), vendorId, searchString);
                  }
              }
              //ning_end

              //List<FamilyInfo> families = SABLL.SearchFamilies(searchString);

              //if (families == null)
              //{
              //    SABLL.DoAlert(this);
              //    return;
              //}

              ViewState["data"] = SABLL.GetFamilyImageByFamilies(families);
              bindGridData(true);
          }
          else 
          {
              List<FamilyInfo> families = SABLL.SearchFamilies(searchString);
              ViewState["data"] = SABLL.GetFamilyImageByFamilies(families);
              bindGridData(true);
          }
          
      }
      catch (System.Exception ex)
      {
          SABLL.ShowInfoMsg(this, ex.Message);


      }
      finally
      {
      }
  }


  //protected void RadSlider1_ValueChanged(object sender, EventArgs e)
  //{
  //    var selectedValue = ((RadSlider)sender).Value;

  //    if (selectedValue == 1m)
  //    {
  //        ImageHeight = Unit.Pixel(150);
  //        ImageWidth = Unit.Pixel(150);
  //        listView_Families.PageSize = 20;
  //    }
  //    else if (selectedValue == 2m)
  //    {
  //        ImageHeight = Unit.Pixel(200);
  //        ImageWidth = Unit.Pixel(200);
  //        listView_Families.PageSize = 10;
  //    }
  //    else if (selectedValue == 3m)
  //    {
  //        ImageHeight = Unit.Pixel(350);
  //        ImageWidth = Unit.Pixel(350);
  //        listView_Families.PageSize = 6;
  //    }

  //    listView_Families.CurrentPageIndex = 0;

  //    listView_Families.Rebind();
  //}

  protected Unit ImageWidth
  {
      get
      {
          object state = ViewState["ImageWidth"] ?? Unit.Pixel(100);
          return (Unit)state;
      }
      private set { ViewState["ImageWidth"] = value; }
  }

  protected Unit ImageHeight
  {
      get
      {
          object state = ViewState["ImageHeight"] ?? Unit.Pixel(100);
          return (Unit)state;
      }
      private set { ViewState["ImageHeight"] = value; }
  }

  protected string CreateWindowScript(Object info)
  {
      //HttpContext.Current.Session["familyId"] = ((SAImageInfo)info).Id;
      //return null;
      //return string.Format("var win = window.radopen('FamilyDetailFrm.aspx?familyId={0}','Details');win.setSize(1000, 700); win.center();", ((SAImageInfo)info).Id);
      return string.Format("var win = window.open('FamilyDetailFrm.aspx?familyId={0}','Details');", ((SAImageInfo)info).Id);
      
      //return string.Format("alert('您选择的零件是：{0}');", ((SAImageInfo)info).Name);
  }

  protected void TextBox1_TextChanged(object sender, EventArgs e)
  {
      var textBox = (TextBox)sender;
      var keyValue = (int)((RadListViewDataItem)textBox.NamingContainer).GetDataKeyValue("Id");
    //  DataProvider.Update(keyValue, textBox.Text.Trim());
  }
  
}